package com.xunyji.xunyjitest.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * @author AltEnter
 * @create 2019-01-10 21:15
 * @desc 基础Bean创建类
 **/
@Configuration
public class CreatBeanConfig {

    // 解决RestTemplate乱码：https://blog.csdn.net/papamilk/article/details/80000683
//    https://www.jianshu.com/p/ca94d9617308
//    https://www.jianshu.com/p/9d2db9b65393
    @Bean
    public RestTemplate restTemplate() {
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setReadTimeout(30000);
        factory.setConnectTimeout(15000);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters()
                .set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        restTemplate.setRequestFactory(factory);
//        RestTemplate restTemplate = new RestTemplate();
        return restTemplate;
    }

}

